<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
    <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="Horace">




    <meta name="keywords" content="zhaohaihao,赵海豪,海豪,Horace,Horace's Blog,海豪的博客,Java,后端">



    <title>设计模式之创建型-工厂方法模式 (Factory Method) | Horace&#39;s Blog</title>
    



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    
    <script src="/js/custom.js"></script>
    



    
    
        
    


    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="Horace" type="application/atom+xml">
</head>
<body>
	<script type="text/javascript" src="/js/canvas-colorful-nest.js"></script>
    <div class="wrapper">
        <header>
    
        <nav class="navbar">
            <div class="container">
                <div class="navbar-header header-logo"><a href="/">Horace&#39;s Blog</a></div>
                <div class="menu navbar-right">
                    
                        <a class="menu-item" href="/archives">归档</a>
                    
                        <a class="menu-item" href="/categories">分类</a>
                    
                        <a class="menu-item" href="/tags">标签</a>
                    
                        <a class="menu-item" href="/links">友链</a>
                    
                        <a class="menu-item" href="/about">关于</a>
                    
                    <input id="switch_default" type="checkbox" class="switch_default">
                    <label for="switch_default" class="toggleBtn"></label>
                </div>
            </div>
        </nav>

        
        <nav class="navbar-mobile" id="nav-mobile">
            <div class="container">
                <div class="navbar-header">
                    <div>
                        <a href="/">Horace&#39;s Blog</a>
                    </div>
                    <div style="display: inherit;">
                        <div><a id="mobile-toggle-theme">Light</a></div>
                        <div class="menu-toggle" onclick="mobileBtn()" style="padding-left: 15px;">&#9776;菜单</div>
                    </div>
                    
                </div>
                <div class="menu" id="mobile-menu">
                    
                        <a class="menu-item" href="/archives">归档</a>
                    
                        <a class="menu-item" href="/categories">分类</a>
                    
                        <a class="menu-item" href="/tags">标签</a>
                    
                        <a class="menu-item" href="/links">友链</a>
                    
                        <a class="menu-item" href="/about">关于</a>
                    
                </div>
            </div>
        </nav>
    
</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
           mobileMenu.classList.remove("active")
        }else{
           toggleMenu.classList.add("active")
           mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <!--<a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>-->
        <a onclick="go_top()">返回页首</a>
        <a onclick="go_bottom()">转到底部</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">设计模式之创建型-工厂方法模式 (Factory Method)</h1>
            
                <div class="post-meta">
                    <i class="iconfont icon-calendar"></i> 发表于 2020-02-10
                    &nbsp;|&nbsp;
                    
                        <span class="post-category">
                        <i class="iconfont icon-folder"></i> 分类于 
                            
                                <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/"> 设计模式</a>
                            
                        </span>
                    &nbsp;|&nbsp;
                    
                    <span id="busuanzi_container_page_pv">
                    <i class="iconfont icon-eye"></i> 阅读量 <span id="busuanzi_value_page_pv"></span> 次
                    </span>

                    <!-- | 
                    1k 字
                     |
                    4 分钟
                    -->
                <!--
                    
                        <a itemprop="author" rel="author" href="/">Horace</a> 
                    

                    
                        <span class="post-time">
                        发布于 <a href="#">02-10&nbsp;14:44:15</a> ,
                        </span>
                    
                    
                        <span class="post-category">
                            
                                <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/"># 设计模式</a>
                            
                        </span>
                    
                -->
                </div>
            
        </header>

        <div class="post-content">
            <p><a href="https://emojipedia.org/backhand-index-pointing-right/" target="_blank" rel="noopener">👉</a><a href="https://github.com/zhaohaihao/java-design-patterns/tree/master/102-Factory-Method" target="_blank" rel="noopener"><strong>文章示例代码</strong></a><a href="https://emojipedia.org/backhand-index-pointing-left/" target="_blank" rel="noopener">👈</a></p>
<h1 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h1><blockquote>
<p>定义一个创建对象的接口，但让实现这个接口的类来决定实例化哪个类。</p>
</blockquote>
<p>工厂方法模式让类的实例化推迟到子类中进行。</p>
<h1 id="场景示例"><a href="#场景示例" class="headerlink" title="场景示例"></a>场景示例</h1><p>笔者这里通过<a href="https://zhaohaihao.com/posts/2020021001.html" target="_blank" rel="noopener">简单工厂模式</a>中示例的演变来进行讲解。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 手机工厂类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 11:09</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PhoneFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取手机</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> type 手机品牌类型</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Phone <span class="title">getPhone</span><span class="params">(String type)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"iphone"</span>.equalsIgnoreCase(type)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> IPhone();</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">"miphone"</span>.equalsIgnoreCase(type)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> MiPhone();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们可以看到<a href="https://zhaohaihao.com/posts/2020021001.html" target="_blank" rel="noopener">简单工厂模式</a>中的手机工厂类并没有子类，创建手机品牌对象都在该工厂类中进行。而工厂模式说的是将创建的职能移交给子类。</p>
<p>下面对代码进行改造，让子类来负责具体手机品牌对象的实例化。</p>
<h2 id="创建基类"><a href="#创建基类" class="headerlink" title="创建基类"></a>创建基类</h2><p>这里仍与<a href="https://zhaohaihao.com/posts/2020021001.html" target="_blank" rel="noopener">简单工厂模式</a>中保持一致。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 手机接口</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 11:04</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Phone</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 拨打电话</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="创建实现类"><a href="#创建实现类" class="headerlink" title="创建实现类"></a>创建实现类</h2><p>这里仍与<a href="https://zhaohaihao.com/posts/2020021001.html" target="_blank" rel="noopener">简单工厂模式</a>中保持一致。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 苹果手机</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 11:07</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IPhone</span> <span class="keyword">implements</span> <span class="title">Phone</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"使用苹果手机拨打电话"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 小米手机</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 11:08</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MiPhone</span> <span class="keyword">implements</span> <span class="title">Phone</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"使用小米手机拨打电话"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="创建抽象工厂接口"><a href="#创建抽象工厂接口" class="headerlink" title="创建抽象工厂接口"></a>创建抽象工厂接口</h2><p>这里对应做了调整，此时获取手机该方法也不需要入参，具体调用哪个实现全部交由客户端选择。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 手机工厂接口</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 11:09</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">PhoneFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取手机</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">Phone <span class="title">getPhone</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="创建工厂实现类"><a href="#创建工厂实现类" class="headerlink" title="创建工厂实现类"></a>创建工厂实现类</h2><p>创建具体实现类实例化的工厂类，同时实现抽象工厂接口。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 苹果手机工厂类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 15:12</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IPhoneFactory</span> <span class="keyword">implements</span> <span class="title">PhoneFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Phone <span class="title">getPhone</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> IPhone();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 小米手机工厂类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 15:12</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MiPhoneFactory</span> <span class="keyword">implements</span> <span class="title">PhoneFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Phone <span class="title">getPhone</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> MiPhone();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="测试类及输出"><a href="#测试类及输出" class="headerlink" title="测试类及输出"></a>测试类及输出</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhh</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> 测试类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2020-02-10 11:11</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        PhoneFactory phoneFactory = <span class="keyword">new</span> IPhoneFactory();</span><br><span class="line">        Phone phone = phoneFactory.getPhone();</span><br><span class="line">        phone.call();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试类的输出结果如下：</p>
<blockquote>
<p>使用苹果手机拨打电话</p>
</blockquote>
<h2 id="类结构图"><a href="#类结构图" class="headerlink" title="类结构图"></a>类结构图</h2><p>以上示例类的结构图如下所示</p>
<p><img src="https://oss.zhaohaihao.com/photo/design-patterns/factory/factory01.png?x-oss-process=style/style01" alt="image.png"></p>
<p>这里 <code>Phone</code> 有两个子类 <code>IPhone</code> 和 <code>MiPhone</code> ，它们处于同一个产品等级。</p>
<p><code>IPhoneFactory</code> 工厂只创建 <code>IPhone</code> ， <code>MiPhoneFactory</code> 工厂只创建 <code>MiPhone</code> 。</p>
<h1 id="扩展说明"><a href="#扩展说明" class="headerlink" title="扩展说明"></a>扩展说明</h1><p>通过工厂方法模式的场景示例我们可以发现，当需要新增加产品时，我们只需要增加一个对应产品的实体类和一个创建对应产品实体类的工厂即可。</p>
<p>举个例子，比如在上述的示例中增加华为手机。</p>
<p>我们只需创建一个新的产品实体类 <code>HuaweiPhone</code> 来实现 <code>IPhone</code> 接口，同时再新建一个创建对应产品实体类的工厂 <code>HuaweiPhoneFactory</code> 来实现 <code>PhoneFactory</code> 接口即可。</p>
<p>这样就不用再对原本的工厂类进行修改，很好的符合了开闭原则。</p>
<h1 id="抽象角色"><a href="#抽象角色" class="headerlink" title="抽象角色"></a>抽象角色</h1><p>我们可以从上述示例中抽象出四种角色(忽略客户端)：</p>
<ul>
<li>产品( <code>Phone</code> )</li>
<li>具体的产品( <code>IPhone</code> 、 <code>MiPhone</code> )</li>
<li>创建者( <code>PhoneFactory</code> )</li>
<li>具体的创建者( <code>IPhoneFactory</code> 、 <code>MiPhoneFactory</code> )</li>
</ul>
<h1 id="源码中的用例"><a href="#源码中的用例" class="headerlink" title="源码中的用例"></a>源码中的用例</h1><ul>
<li><code>java.util.Collection</code> 类中的 <code>iterator()</code> 方法</li>
<li><code>Logback</code> 中的 <code>org.slf4j.ILoggerFactory</code></li>
</ul>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><h2 id="适用场景"><a href="#适用场景" class="headerlink" title="适用场景"></a>适用场景</h2><ul>
<li>创建对象需要大量重复的代码</li>
<li>客户端不依赖于产品类实例如何被创建、实现等细节</li>
<li>一个类通过其子类来指定创建哪个对象</li>
</ul>
<h2 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h2><ul>
<li>用户只需要关心所需产品对应的工厂，无须关心创建细节</li>
<li>加入新产品是符合开闭原则的，提高可扩展性</li>
</ul>
<h2 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h2><ul>
<li>类的个数容易过多，增加复杂度</li>
<li>增加了系统的抽象性和理解难度</li>
</ul>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li>《Head First 设计模式》</li>
<li>《大话设计模式》</li>
<li><a href="https://zh.wikipedia.org/wiki/%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95" target="_blank" rel="noopener">维基百科-工厂方法</a></li>
</ul>

        </div>

        
            <section class="post-copyright">
            
                
                
                
                

                
                    <p class="copyright-item" style="text-align: center; margin: 20px 0px;">
                        <span>© 转载请保留本文地址, 著作权归作者所有</span>
                    </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <!--<span>Tag(s):</span>-->
                <span class="tag">
                    
                    
                      <i class="iconfont icon-tag"></i> <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">返回上层</a>
                <span>· </span>
                <a href="/">返回主页</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/posts/2020021101.html">设计模式之创建型-抽象工厂模式 (Abstract Factory)</a>
            
            
            <a class="next" rel="next" href="/posts/2020021001.html">设计模式之创建型-简单工厂模式 (Simple Factory)</a>
            
        </section>


    </article>
</div>

        </div>
        
	<footer id="footer" class="footer" style="line-height: unset;">
	    <div class="copyright" style="text-align: center;">
	        本站已安全运行 <span id="htmer_time">0年293天1时59分51秒</span></br>
            <span id="busuanzi_container_site_pv">总访问量 <span id="busuanzi_value_site_pv"></span> 次</span>
            |
            <span id="busuanzi_container_site_uv">总访客数 <span id="busuanzi_value_site_uv"></span> 人</span>
            |
            总字数 42.7k 字  <a href="https://zhaohaihao.com/atom.xml" target="_blank"><i class="iconfont icon-RSS"></i></a> </br>
	        <span>© 2019-2020 Horace&#39;s Blog.</span> <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow">浙ICP备18055264号</a>
	    </div>
	</footer>

    <style>
        .icon-RSS {
            color: #FD9B00;
        }
    </style>

	<script language="javascript">
    function siteTime(){
        window.setTimeout("siteTime()", 1000);
        var seconds = 1000;
        var minutes = seconds * 60;
        var hours = minutes * 60;
        var days = hours * 24;
        var years = days * 365;
        var today = new Date();
        var todayYear = today.getFullYear();
        var todayMonth = today.getMonth()+1;
        var todayDate = today.getDate();
        var todayHour = today.getHours();
        var todayMinute = today.getMinutes();
        var todaySecond = today.getSeconds();
        /* Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
        year - 作为date对象的年份，为4位年份值
        month - 0-11之间的整数，做为date对象的月份
        day - 1-31之间的整数，做为date对象的天数
        hours - 0(午夜24点)-23之间的整数，做为date对象的小时数
        minutes - 0-59之间的整数，做为date对象的分钟数
        seconds - 0-59之间的整数，做为date对象的秒数
        microseconds - 0-999之间的整数，做为date对象的毫秒数 */
        var t1 = Date.UTC(2019,6,12,22,22,22); //北京时间2016-12-1 00:00:00
        var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
        var diff = t2-t1;
        var diffYears = Math.floor(diff/years);
        var diffDays = Math.floor((diff/days)-diffYears*365);
        var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
        var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
        var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
        document.getElementById("htmer_time").innerHTML=diffYears+"年"+diffDays+"天"+diffHours+"时"+diffMinutes+"分"+diffSeconds+"秒";
    }
    siteTime();
</script>

    </div>
</body>
</html>
